package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.service.CheckGroupService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @author 吴
 * @version V1.0
 * @Description
 * @Package com.itheima.controller
 * @date 2021/11/22 18:16
 */
@RestController
@RequestMapping("/checkGroup")
public class CheckGroupController {

    @Reference
    private CheckGroupService checkGroupService;

    @GetMapping("/findAll")
    public Result findAll(){
        try {
            List<CheckGroup> groupList = checkGroupService.findAll();
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,groupList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }

    /**
     * 分页查询检查组
     * @param queryPageBean
     * @return
     */
    @GetMapping("/findPage")
    public Result findPage(QueryPageBean queryPageBean){
        try {
            PageResult result = checkGroupService.selectByCondition(queryPageBean);
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,result);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }

    /**
     * 添加检查组
     * @param checkGroup  检查组
     * @param checkItemIds  检查组下的检查项
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody CheckGroup checkGroup, @RequestParam("checkItemIds") Integer[] checkItemIds){
        try {
            checkGroupService.add(checkGroup,checkItemIds);
            return new Result(true,MessageConstant.ADD_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_CHECKGROUP_FAIL);
        }
    }

    /**
     * 根据检查组id查询检查组信息
     * @param checkGroupId
     * @return
     */
    @GetMapping("/findById/{checkGroupId}")
    public Result findById(@PathVariable Integer checkGroupId){
        try {
            CheckGroup checkGroup=checkGroupService.findCheckGroupById(checkGroupId);
            return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkGroup);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }

    /**
     * 根据检查组id查询关联的检查项id
     * @param checkGroupId  当前检查组下的检查项
     * @return
     */
    @GetMapping("/findCheckItemIdsByCheckGroupId/{checkGroupId}")
    public Result findCheckItemIdsByCheckGroupId(@PathVariable Integer checkGroupId){
        try {
            Integer[] checkItemIds=checkGroupService.findCheckItemIdsByCheckGroupId(checkGroupId);
            return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkItemIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }

    /**
     * 更新检查组及其检查项对应的关系
     * @param checkGroup        检查组
     * @param checkItemIds      当前检查组下的检查项
     * @return
     */
    @PutMapping("/edit")
    public Result edit(@RequestBody CheckGroup checkGroup, @RequestParam("checkItemIds") Integer[] checkItemIds){
        try {
            checkGroupService.update(checkGroup,checkItemIds);
            return new Result(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
    }

    /**
     * 删除检查组及其检查项对应的关系
     * @param checkGroupId  检查组Id
     * @return
     */
    @DeleteMapping("/{checkGroupId}")
    public Result delete(@PathVariable Integer checkGroupId){
        try {
            checkGroupService.deleteById(checkGroupId);
            return new Result(true,MessageConstant.DELETE_CHECKGROUP_SUCCESS);
        }catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false,e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_CHECKGROUP_FAIL);
        }
    }



}
